<template>
  <div class="newCourse">
    <div
      class="dialog animated"
      :class="open? 'fadeIn':'fadeOut'"
      v-show="show"
      :style="{visibility: (visible?'visible':'hidden')}"
    >
      <div class="panel animated" :class="open? 'zoomIn':''">
        <div class="title">
          {{title}}
          <div class="close" @click="cancel()"></div>
        </div>
        <div>
          <el-form label-width="140px" class="ruleForm">
            <el-form-item label="课程类型" :rules="[
      { required: true, message: ''}]">
              <el-select
                v-model="curriculumInfo.courseType"
                placeholder="请选择课程类型"
                @change="courseModeChange"
                :disabled="curriculumInfo.isOpeningClassStatus"
                style="width:45%"
              >
                <el-option
                  v-for="item in curriculumInfo.courseTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- 课程类型说明 !-->
            <el-form-item
              style="margin-top: -0.2rem;margin-bottom:0rem;"
              v-if="curriculumInfo.courseType != ''"
            >
              <div v-if="curriculumInfo.courseType == 5">
                <span style="color:red;">*</span>
                指学员报名后，通过设备在线学习来完成课程。
              </div>
              <div v-else-if="curriculumInfo.courseType == 2">
                <span style="color:red;">*</span>
                一个或多个老师对应多名学员，有固定的排课计划。全天或半天开课，签到按天进行
              </div>
              <div v-else-if="curriculumInfo.courseType == 3">
                <span style="color:red;">*</span>
                班级的课程是按课时来上，即一天只有一个或两个小时。签到按小时或分钟进行
              </div>
              <div v-else-if="curriculumInfo.courseType == 4">
                <span style="color:red;">*</span>
                指一堂课里有线上和线下两位教师上课
              </div>
            </el-form-item>

            <el-form-item
              :label="curriculumInfo.courseType==2 || isOrg?'机构课程':'自定义课程'"
              :rules="[
      { required: true, message: ''}]"
            >
              <div v-for="item,index in curriculumInfo.libraryCourseList">
                <el-select
                  v-if="item.attributionType !=2"
                  v-model="item.libraryCourseId"
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  @change="selectLibraryCourse($event,index)"
                  placeholder="请选择机构课程"
                  style="width:45%; "
                >
                  <el-option
                    v-for="item in courseLibraryList"
                    :key="item.libraryCourseId"
                    :label="item.libraryCourseName"
                    :value="item.libraryCourseId"
                  >{{getLabel(item)}}</el-option>
                </el-select>

                <div
                  class="charge_item"
                  v-if="item.libraryCourseId == null || (item.attributionType ==2 && item.libraryCourseId !='')"
                >
                  <el-input
                    style="width:15%"
                    maxlength="9"
                    :disabled="curriculumInfo.isOpeningClassStatus || !item.isAdd"
                    v-model="item.libraryCourseName"
                    placeholder="课程名称"
                  ></el-input>&nbsp;
                  <el-input-number
                    size="small"
                    style="width:12%"
                    controls-position="right"
                    class="newclassroomNumber"
                    :disabled="curriculumInfo.isOpeningClassStatus || !item.isAdd"
                    v-model="item.syllabusNum"
                    @change="syllabusNumChange"
                    placeholder="课节数"
                    :min="1"
                    :max="999"
                  ></el-input-number>&nbsp;课时&nbsp;
                  <el-input-number
                    size="small"
                    style="width:12%"
                    controls-position="right"
                    class="newclassroomNumber"
                    :disabled="curriculumInfo.isOpeningClassStatus || !item.isAdd"
                    v-model="item.perSyllabusDuration"
                    :min="1"
                    :max="999"
                  ></el-input-number>&nbsp;分钟/课时
                  <span
                    style="margin-left: 0.15rem;"
                    v-if="curriculumInfo.courseType == 2 && !curriculumInfo.isOpeningClassStatus"
                    class="add_btn"
                    @click="addCourse()"
                  ></span>
                  <span
                    v-if="!curriculumInfo.isOpeningClassStatus"
                    class="reduce_btn"
                    @click="deleteCourse(index)"
                  ></span>
                </div>
                <span v-else>
                  <span
                    v-if="curriculumInfo.courseType == 2 && !curriculumInfo.isOpeningClassStatus"
                    class="add_btn"
                    @click="addCourse()"
                  ></span>
                  <span
                    v-if="curriculumInfo.courseType == 2 && !curriculumInfo.isOpeningClassStatus"
                    class="reduce_btn"
                    @click="deleteCourse(index)"
                  ></span>
                </span>
                <div
                  v-if="item.syllabusNum != '' && item.libraryCourseId != null && item.attributionType !=2"
                  style="margin-left:0.02rem;font-size:0.16rem;margin-top:0rem;"
                >
                  共{{item.syllabusNum}}课时,所属科目:{{item.subjectName}}{{item.subSubjectName == null ?"":"/"+item.subSubjectName}},建议上课时长{{item.oneSyllabusDuration}}分钟/课时,
                  <label>
                    校区上课时长
                    <el-input-number
                      size="small"
                      style="width:12%"
                      controls-position="right"
                      class="newclassroomNumber"
                      :disabled="curriculumInfo.isOpeningClassStatus"
                      v-model="item.perSyllabusDuration"
                    ></el-input-number>分钟/课时
                  </label>
                </div>
                <!-- <span
                  
                >{{item.syllabusNum}}课时</span>-->
                <!-- 当选择的库课程为自定义时 !-->
              </div>
            </el-form-item>
            <el-form-item label="所属科目" v-if="curriculumInfo.courseType == 2">
              <el-cascader
                style="width:45%;"
                v-model="selectSubjectId"
                :options="curriculumInfo.subjectList"
                :disabled="curriculumInfo.isOpeningClassStatus"
                placeholder="请选择所属科目"
                @change="subjectChange"
                clearable
              ></el-cascader>
            </el-form-item>
            <el-form-item
              v-if="(curriculumInfo.courseType ==2) || (curriculumInfo.courseType !=2 && isOrg)"
              label="课程名称"
              :rules="[
      { required: true, message: ''}]"
            >
              <el-input
                v-model="curriculumInfo.courseName"
                maxlength="30"
                style="width:45%"
                placeholder="请输入校区课程名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="适用年龄">
              <el-select
                v-model="curriculumInfo.minSuitableAge"
                @change="ageStartChange()"
                style="width: 1.75rem;"
                placeholder="开始年龄"
                clearable
              >
                <el-option
                  v-for="item in ageStartList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                  v-show="item.show"
                ></el-option>
              </el-select>&nbsp;&nbsp;—&nbsp;
              <el-select
                v-model="curriculumInfo.maxSuitableAge"
                @change="ageEndChange()"
                style="width: 1.75rem;"
                placeholder="结束年龄"
                clearable
              >
                <el-option
                  v-for="item in ageEndList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                  v-show="item.show"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div class="line_from"></div>
          <el-form ref="form2" label-width="140px" class="ruleForm">
            <!--   <el-form-item label="授课模式" prop="courseMode">
              <el-radio-group v-model="curriculumInfo.courseMode">
                <el-radio
                  border
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  v-for="item in curriculumInfo.courseModeList"
                  :key="item.value"
                  :label="item.value"
                >{{ item.name }}</el-radio>
              </el-radio-group>
            </el-form-item>-->
            <el-form-item label="开课校区" :rules="[
      { required: true, message: ''}]">
              <el-radio-group
                v-model="curriculumInfo.classtext"
                @change="handleCheckedCitiesChange"
              >
                <el-radio border :disabled="curriculumInfo.isOpeningClassStatus" :label="1">本校区</el-radio>
                <!-- <el-radio border :disabled="curriculumInfo.isOpeningClassStatus" :label="2">指定校区</el-radio> -->
                <!-- 825只能选择本校 -->
              </el-radio-group>
              <el-select
                v-model="curriculumInfo.selectSchoolList"
                multiple
                collapse-tags
                :disabled="curriculumInfo.isOpeningClassStatus"
                style="margin-left: 20px;"
                placeholder="请选择校区"
                v-if="curriculumInfo.isShowSchoolView"
                @change="schoolChange"
              >
                <el-option
                  v-for="item in curriculumInfo.classList"
                  :key="item.campusId"
                  :label="item.name"
                  :value="item.campusId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="收费模式" :rules="[
      { required: true, message: ''}]">
              <el-radio-group
                v-model="curriculumInfo.chargeMode"
                @change="handleradioChange"
                v-if="curriculumInfo.courseType == 2"
              >
                <el-radio
                  border
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  v-for="item in activeChargeModeList"
                  :key="item.name"
                  :label="item.value"
                >{{ item.name }}</el-radio>
              </el-radio-group>
              <el-radio-group
                v-model="curriculumInfo.chargeMode"
                @change="handleradioChange"
                v-else-if="curriculumInfo.courseType == 4"
              >
                <el-radio
                  border
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  v-for="item in activeModeList"
                  :key="item.name"
                  :label="item.value"
                >{{ item.name }}</el-radio>
              </el-radio-group>
              <el-radio-group
                v-model="curriculumInfo.chargeMode"
                @change="handleradioChange"
                v-else
              >
                <el-radio
                  border
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  v-for="item in chargeModeList"
                  :key="item.name"
                  :label="item.value"
                >{{ item.name }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="定价标准"
              v-if="curriculumInfo.chargeMode != ''"
              :rules="[
      { required: true, message: ''}]"
            >
              <span v-for="item in curriculumInfo.schoolText" class="school_item">{{ item }}</span>
            </el-form-item>
            <!-- 定价标准根据选择的课程类型区分 !-->
            <el-form-item v-if="curriculumInfo.chargeMode != ''">
              <div v-if="curriculumInfo.chargeMode == '1' && curriculumInfo.courseType == 4">
                <div v-for="item in curriculumInfo.coursePackageEntityList" class="charge_item">
                  <span v-clipboard:copy="item.coursePackageId">按课时：</span>
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    @change="handleChange(item)"
                    style="width:12%;"
                    :disabled="curriculumInfo.isOpeningClassStatus  && !item.isErasable"
                    v-model="item.syllabusNum"
                    :min="1"
                    :max="maxCourseSum"
                  ></el-input-number>
                  <span style="width:25%">课时 =</span>
                  <!-- <el-input style="width:12%;" :disabled="true" v-model="item.courseHour"></el-input>
                  <span style="width:25%">课时 =</span>-->
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:15%;"
                    v-model="item.totalMoney"
                    :precision="2"
                    :step="0.01"
                    :min="0"
                    :max="99999"
                  ></el-input-number>&nbsp;&nbsp;元
                  <span class="add_btn" @click="addPrice(curriculumInfo.chargeMode)"></span>
                  <span class="reduce_btn" v-if="item.isErasable" @click="deletePrice(item)"></span>
                </div>
              </div>
              <div v-else-if="curriculumInfo.chargeMode == '1' && curriculumInfo.courseType != 4">
                <div v-for="item in curriculumInfo.coursePackageEntityList" class="charge_item">
                  <span v-clipboard:copy="item.coursePackageId">按课时：</span>
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    @change="handleChange(item)"
                    style="width:12%;"
                    :disabled="curriculumInfo.isOpeningClassStatus  && !item.isErasable"
                    v-model="item.syllabusNum"
                    :min="1"
                    :max="maxCourseSum"
                  ></el-input-number>
                  <span style="width:25%">课时 =</span>
                  <!-- <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:12%;"
                    :disabled="curriculumInfo.isOpeningClassStatus && !item.isErasable"
                    v-model="item.courseHour"
                    :min="1"
                    :max="9999"
                  ></el-input-number>
                  <span style="width:25%">课时 =</span>-->
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:15%;"
                    v-model="item.totalMoney"
                    :precision="2"
                    :step="0.01"
                    :min="0"
                    :max="99999"
                  ></el-input-number>&nbsp;&nbsp;元
                  <span class="add_btn" @click="addPrice(curriculumInfo.chargeMode)"></span>
                  <span class="reduce_btn" v-if="item.isErasable" @click="deletePrice(item)"></span>
                </div>
              </div>
              <div v-else-if="curriculumInfo.chargeMode == '2'">
                <div v-for="item in curriculumInfo.coursePackageEntityList" class="charge_item">
                  <span v-clipboard:copy="item.coursePackageId">按期：</span>
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:12%;"
                    @change="handleChange(item)"
                    :disabled="curriculumInfo.isOpeningClassStatus"
                    v-model="item.syllabusNum"
                    :min="1"
                    :max="maxCourseSum"
                  ></el-input-number>
                  <span style="width:25%">课时 =</span>
                  <!-- <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:12%;"
                    :disabled="curriculumInfo.isOpeningClassStatus"
                    v-model="item.courseHour"
                    :min="1"
                    :max="9999"
                  ></el-input-number>
                  <span style="width:25%">课时 =</span>-->
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:15%;"
                    v-model="item.totalMoney"
                    :precision="2"
                    :step="0.01"
                    :min="0"
                    :max="99999"
                  ></el-input-number>&nbsp;&nbsp;元/期
                </div>
              </div>
              <div v-else-if="curriculumInfo.chargeMode == '3'">
                <div v-for="item in curriculumInfo.coursePackageEntityList" class="charge_item">
                  <span v-clipboard:copy="item.coursePackageId">按时间：</span>
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:12%;"
                    v-model="item.courseHour"
                    :disabled="curriculumInfo.isOpeningClassStatus && !item.isErasable"
                    :min="1"
                    :max="9999"
                  ></el-input-number>
                  <el-select
                    v-model="item.courseTimeMode"
                    type="number"
                    :disabled="curriculumInfo.isOpeningClassStatus && !item.isErasable"
                    placeholder="请选择"
                    style="width:10%; "
                  >
                    <el-option
                      v-for="item in curriculumInfo.courseTimeModeList"
                      :key="item.name"
                      :label="item.name"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                  <span>=</span>
                  <el-input-number
                    size="small"
                    controls-position="right"
                    class="newclassroomNumber"
                    style="width:15%;"
                    v-model="item.totalMoney"
                    :precision="2"
                    :step="0.01"
                    :min="0"
                    :max="99999"
                  ></el-input-number>&nbsp;&nbsp;元
                  <span class="add_btn" @click="addPrice(curriculumInfo.chargeMode)"></span>
                  <span class="reduce_btn" v-if="item.isErasable" @click="deletePrice(item)"></span>
                </div>
              </div>
            </el-form-item>
          </el-form>
          <div class="line_from"></div>
          <el-form ref="form3" class="ruleForm">
            <el-form-item
              label="课时消耗设定"
              label-width="174px"
              :rules="[
      { required: true, message: ''}]"
            ></el-form-item>
            <el-form-item label-width="100px">
              <div v-if="curriculumInfo.courseType == 4 || curriculumInfo.courseType == 3">
                <el-radio
                  v-model="curriculumInfo.consumeMode"
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  :label="1"
                >按签到核算消课</el-radio>请假
                <el-input-number
                  size="small"
                  controls-position="right"
                  class="newclassroomNumber"
                  style="width:20%;margin:0 10px;"
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  v-model="curriculumInfo.offsetDay"
                  :min="1"
                  :max="9999"
                ></el-input-number>次核算消课
              </div>
              <div>
                <el-radio
                  v-model="curriculumInfo.consumeMode"
                  :disabled="curriculumInfo.isOpeningClassStatus"
                  :label="2"
                >按开课时间核算消课</el-radio>
              </div>
            </el-form-item>
          </el-form>
        </div>
        <div class="btn_div">
          <div class="btn_sure" @click="submitForm()">确 认</div>
          <div class="btn_cancel" @click="cancel()">取 消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
var vm;
export default {
  data: function() {
    return {
      show: false,
      open: false,
      visible: false,
      isAdd: false,
      title: "",
      isOrg: true, //是否是机构课程
      chargeModeList: [],
      curriculumInfo: {},
      ageStartList: [], //筛选适用年龄数据
      ageEndList: [], //筛选适用年龄数据
      courseLibraryList: [], //库课程列表
      maxCourseSum: 99999,
      selectSubjectId: [], //筛选科目的数组
      selectObject: {
        //筛选库课程列表
        campusId: "",
        subjectId: "",
        subSubjectId: "",
        courseType: ""
      }
    };
  },
  methods: {
    /**自定义库课程课节 变化回调 */
    syllabusNumChange(val) {
      vm.maxCourseSum = val;
    },
    getLabel(item) {
      if (item.subSubjectName == null) {
        if (item.attributionType == 2) {
          return item.libraryCourseName;
        } else {
          return (
            item.libraryCourseName +
            "  (  " +
            item.subjectName +
            " | 共" +
            item.syllabusNum +
            "课时 | 每课时" +
            item.oneSyllabusDuration +
            "分钟  )"
          );
        }
      } else {
        return (
          item.libraryCourseName +
          "  (  " +
          item.subjectName +
          "/" +
          item.subSubjectName +
          " | 共" +
          item.syllabusNum +
          "课时 | 每课时" +
          item.oneSyllabusDuration +
          "分钟  )"
        );
      }
    },
    /**选择库课程 */
    selectLibraryCourse(val, index) {
      if (val != null) {
        vm.isOrg = true;
        for (let i = 0; i < vm.courseLibraryList.length; i++) {
          if (vm.courseLibraryList[i].libraryCourseId == val) {
            /**改变当前科目 */
            vm.selectSubjectId = [];
            if (vm.courseLibraryList[i].subSubjectId == null) {
              vm.selectSubjectId.push(vm.courseLibraryList[i].subjectId);
              // vm.selectObject.subjectId = vm.courseLibraryList[i].subjectId;
            } else {
              // vm.selectObject.subjectId = vm.courseLibraryList[i].subjectId;
              // vm.selectObject.subSubjectId =
              //   vm.courseLibraryList[i].subSubjectId;
              vm.selectSubjectId.push(vm.courseLibraryList[i].subjectId);
              vm.selectSubjectId.push(vm.courseLibraryList[i].subSubjectId);
            }
            // vm.curriculumInfo.subjectId = vm.courseLibraryList[i].subjectId;

            api.getCourseLibraryList(vm.selectObject).then(function(data) {
              if (data.result == "操作成功") {
                vm.courseLibraryList = data.list;
              }
            });

            vm.curriculumInfo.minSuitableAge =
              vm.courseLibraryList[i].minSuitableAge;
            vm.curriculumInfo.maxSuitableAge =
              vm.courseLibraryList[i].maxSuitableAge;
            /**拿到当前库课程的信息 */
            vm.curriculumInfo.libraryCourseList[index].libraryCourseName =
              vm.courseLibraryList[i].libraryCourseName;
            vm.curriculumInfo.courseName =
              vm.courseLibraryList[i].libraryCourseName;
            vm.curriculumInfo.libraryCourseList[index].subjectName =
              vm.courseLibraryList[i].subjectName;
            
            vm.curriculumInfo.libraryCourseList[index].subSubjectName =
              vm.courseLibraryList[i].subSubjectName;
            vm.curriculumInfo.libraryCourseList[index].oneSyllabusDuration =
              vm.courseLibraryList[i].oneSyllabusDuration;
              vm.curriculumInfo.libraryCourseList[index].perSyllabusDuration =
              vm.courseLibraryList[i].oneSyllabusDuration;
            vm.maxCourseSum = vm.courseLibraryList[i].syllabusNum;
            vm.curriculumInfo.libraryCourseList[index].syllabusNum =
              vm.courseLibraryList[i].syllabusNum;
            vm.curriculumInfo.libraryCourseList[index].attributionType =
              vm.courseLibraryList[i].attributionType;
            let chargeMode =
              vm.curriculumInfo.coursePackageEntityList[0].chargeMode;
            vm.curriculumInfo.coursePackageEntityList = [];
            vm.curriculumInfo.coursePackageEntityList.push({
              chargeMode: chargeMode,
              courseTimeMode: 1,
              courseHour: vm.maxCourseSum,
              syllabusNum: vm.maxCourseSum,
              cycle: 10,
              totalMoney: "",
              isErasable: true
            });
            break;
          }
        }
      } else {
        // vm.curriculumInfo.libraryCourseList[index] = {
        //   libraryCourseId: null,
        //   libraryCourseName: "",
        //   oneSyllabusDuration: "",
        //   syllabusNum: "",
        //   attributionType: 2
        // };
        vm.isOrg = false;
        vm.curriculumInfo.courseName = "";
        vm.curriculumInfo.libraryCourseList[index].libraryCourseName = "";
        vm.curriculumInfo.libraryCourseList[index].oneSyllabusDuration = "";
        vm.curriculumInfo.libraryCourseList[index].syllabusNum = 1;
        vm.maxCourseSum =
          vm.curriculumInfo.libraryCourseList[index].syllabusNum;
        vm.curriculumInfo.libraryCourseList[index].isAdd = true;
        vm.curriculumInfo.libraryCourseList[index].attributionType = 2;
      }
    },
    /**
     * 科目发生改变
     */
    subjectChange(val) {
      // if (vm.selectSubjectId.length == 1) {
      //   vm.selectObject.subjectId = vm.selectSubjectId[0];
      //   vm.selectObject.subSubjectId = "";
      // } else if (vm.selectSubjectId.length == 2) {
      //   vm.selectObject.subjectId = vm.selectSubjectId[0];
      //   vm.selectObject.subSubjectId = vm.selectSubjectId[1];
      // }else{
      //   vm.selectObject.subjectId = "";
      //   vm.selectObject.subSubjectId = "";
      // }
      // // vm.selectObject.subjectId = val;
      // // debugger;
      // vm.getCourseLibraryList();
    },
    ageStartChange: function() {
      //开始年龄发生改变
      //限制结束年龄
      vm.ageEndList.forEach(function(item) {
        if (
          !vm.curriculumInfo.minSuitableAge ||
          parseInt(item.value) >= parseInt(vm.curriculumInfo.minSuitableAge)
        ) {
          item.show = true;
        } else {
          item.show = false;
        }
      });
      // vm.selectObject.startSuitableAge = vm.curriculumInfo.minSuitableAge;
      // if (vm.selectObject.endSuitableAge != "") {
      //   vm.getCourseLibraryList();
      // }
      // var age = (vm.ageStart || -1) + "/" + (vm.ageEnd || -1);
    },
    ageEndChange: function() {
      //结束年龄发生改变
      //限制开始年龄
      vm.ageStartList.forEach(function(item) {
        if (
          !vm.curriculumInfo.maxSuitableAge ||
          parseInt(item.value) <= parseInt(vm.curriculumInfo.maxSuitableAge)
        ) {
          item.show = true;
        } else {
          item.show = false;
        }
      });
      // vm.selectObject.endSuitableAge = vm.curriculumInfo.maxSuitableAge;
      // if (vm.selectObject.startSuitableAge != "") {
      //   vm.getCourseLibraryList();
      // }
      // var age = (vm.ageStart || -1) + "/" + (vm.ageEnd || -1);
    },
    init: function(data) {
      vm.show = true;
      setTimeout(function() {
        vm.open = true;
        vm.visible = true;
      }, 50);
      let dicdata = tool.getItem(lsKey.dict, true);
      let user = tool.getItem(lsKey.user, true); //得到当前用户信息
      vm.selectObject.campusId = this.$store.state.campusId;
      let courseCampusId = this.$store.state.campusId;
      vm.chargeModeList = dicdata["charge_mode"];
      let courselisttemp = {};
      vm.selectSubjectId = [];
      if (data.courseId == undefined) {
        vm.title = "新建课程";
        vm.isAdd = true;
        courselisttemp = {
          isShowSchool: true,
          courseTypeList: dicdata["course_type"],
          subjectList: [],
          courseModeList: dicdata["course_mode"],
          classtext: 1,
          classList: [],
          minSuitableAge: "",
          maxSuitableAge: "",
          courseTimeModeList: dicdata["course_time_mode"],
          courseName: "",
          courseType: "",
          subjectId: "",
          courseMode: "",
          chargeMode: 1,
          isOpeningClassStatus: false,
          offsetDay: "",
          allowLateMinute: "",
          oneHourDuration: "",
          createOrgId: user.orgId,
          remark: "",
          coursePackageEntityList: [
            {
              chargeMode: 1,
              courseTimeMode: 1,
              courseHour: 1,
              syllabusNum: "",
              cycle: 10,
              totalMoney: "",
              isErasable: true
            }
          ],
          courseCampusEntityList: [],
          isShowSchoolView: false,
          schoolText: ["所有校区"],
          selectSchoolList: [],
          consumeMode: 2,
          libraryCourseList: [
            {
              libraryCourseId: "",
              libraryCourseName: "",
              oneSyllabusDuration: "",
              perSyllabusDuration:"",
              syllabusNum: "",
              attributionType: 1
            }
          ]
        };
        api
          .campusList({
            orgId: user.orgId + ""
          })
          .then(function(data) {
            //枚举校区列表
            if (data.result == "操作成功") {
              courselisttemp.classList = data.list;
              courselisttemp.createOrgId = user.orgId;
              let org = data.list.find(value => {
                return value.campusId == courseCampusId;
              });
              courselisttemp.schoolText = [org.name];
              courselisttemp.courseCampusEntityList = [org];
            }
            return api.getSubjectList({
              orgId: user.orgId + ""
            });
          })
          .then(function(data) {
            //枚举科目列表
            if (data.result == "操作成功") {
              data.list.forEach(item => {
                let subList = [];
                item.subSubjectList.forEach(r => {
                  subList.push({
                    value: r.subSubjectId,
                    label: r.subSubjectName
                  });
                });
                if (subList.length > 0) {
                  courselisttemp.subjectList.push({
                    value: item.subjectId,
                    label: item.name,
                    children: subList
                  });
                } else {
                  courselisttemp.subjectList.push({
                    value: item.subjectId,
                    label: item.name
                  });
                }
              });
            }
            vm.curriculumInfo = courselisttemp;
          });
      } else {
        vm.title = "编辑课程";
        vm.isAdd = false;
        api
          .courseInfo({
            courseId: data.courseId
          })
          .then(function(data) {
            console.log(data);
            if (data.result == "操作成功") {
              courselisttemp = data.info;
              if (courselisttemp.subSubjectId == null) {
                if (courselisttemp.subjectId != "") {
                  vm.selectSubjectId.push(courselisttemp.subjectId);
                }
              } else {
                vm.selectSubjectId.push(courselisttemp.subjectId);
                vm.selectSubjectId.push(courselisttemp.subSubjectId);
                // vm.selectObject.subSubjectId = courselisttemp.subSubjectId;
              }
              courselisttemp.isOpeningClassStatus =
                courselisttemp.isOpeningClass == 0 ? false : true;
              //设置筛选库课程参数，初始化筛选数据
              // vm.selectObject.subjectId = courselisttemp.subjectId;
              vm.selectObject.courseType = courselisttemp.courseType;
              return api.getCourseLibraryList(vm.selectObject);
            }
          })
          .then(function(data) {
            if (data.result == "操作成功") {
              vm.courseLibraryList = data.list;
              // console.log(
              //   vm.getOtherLibraryList(courselisttemp.libraryCourseList)
              // );
              // vm.courseLibraryList = vm.courseLibraryList.concat(
              //   vm.getOtherLibraryList(courselisttemp.libraryCourseList)
              // );
              console.log(vm.courseLibraryList);
            }
            return api.getSubjectList({
              orgId: user.orgId + ""
            });
          })
          .then(function(data) {
            //枚举科目列表
            if (data.result == "操作成功") {
              courselisttemp.subjectList = [];
              data.list.forEach(item => {
                let subList = [];
                item.subSubjectList.forEach(r => {
                  subList.push({
                    value: r.subSubjectId,
                    label: r.subSubjectName
                  });
                });
                if (subList.length > 0) {
                  courselisttemp.subjectList.push({
                    value: item.subjectId,
                    label: item.name,
                    children: subList
                  });
                } else {
                  courselisttemp.subjectList.push({
                    value: item.subjectId,
                    label: item.name
                  });
                }
              });
            }
            return api.campusList({
              orgId: user.orgId + ""
            });
          })
          .then(function(data) {
            //枚举校区列表
            courselisttemp.courseTypeList = dicdata["course_type"];
            courselisttemp.courseModeList = dicdata["course_mode"];
            courselisttemp.courseTimeModeList = dicdata["course_time_mode"];
            courselisttemp.selectSchoolList = [];
            courselisttemp.schoolText = [];
            courselisttemp.libraryCourseList =
              courselisttemp.libraryCourseList.length == 0
                ? [
                    {
                      libraryCourseId: "",
                      libraryCourseName: "",
                      oneSyllabusDuration: "",
                      perSyllabusDuration:"",
                      syllabusNum: "",
                      attributionType: 1
                    }
                  ]
                : courselisttemp.libraryCourseList;
            let index = 0;
            courselisttemp.libraryCourseList.forEach(item => {
              item.isAdd = false;
              index = index < item.syllabusNum ? item.syllabusNum : index;
            });
            console.log(courselisttemp.libraryCourseList);
            vm.maxCourseSum = index;
            if (data.result == "操作成功") {
              courselisttemp.classList = data.list;
              courselisttemp.isShowSchoolView = false;
              courselisttemp.selectSchoolList = [];
              courselisttemp.schoolText = ["本校区"];
              courselisttemp.classtext = 1;
            }
            vm.curriculumInfo = courselisttemp;
          });
      }
    },
    /**获取自定义的库课程 */
    getOtherLibraryList(list) {
      let returnList = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].attributionType == 2) {
          list[i].disabled = true;
          returnList.push(list[i]);
        }
      }
      return returnList;
    },
    //判断两个数组中的某个值是否相同
    getListToList(list1, list2) {
      console.log(list1.length, list2.length);
      if (list1.length != list2.length) return false;
      for (let i = 0; i < list1.length; i++) {
        for (let y = 0; y < list2.length; y++) {
          if (list1[i].campusId == list2[y].campusId) {
            break;
          }
          if (y == list2.length - 1) {
            return false;
          }
        }
      }
      return true;
    },
    //获取筛选的库课程列表
    getCourseLibraryList() {
      vm.curriculumInfo.libraryCourseList = [
        {
          libraryCourseId: "",
          libraryCourseName: "",
          oneSyllabusDuration: "",
          perSyllabusDuration:"",
          syllabusNum: "",
          attributionType: 1
        }
      ];
      console.log(vm.selectObject);
      api.getCourseLibraryList(vm.selectObject).then(function(data) {
        if (data.result == "操作成功") {
          vm.courseLibraryList = data.list;
        }
      });
    },
    //课程类型发生改变
    courseModeChange(val) {
      vm.isOrg = true;
      if (val == 2) {
        this.curriculumInfo.chargeMode = 3;
        this.curriculumInfo.coursePackageEntityList = [];
        this.curriculumInfo.coursePackageEntityList.push({
          chargeMode: 3,
          courseTimeMode: 1,
          courseHour: 1,
          syllabusNum: "",
          cycle: 10,
          totalMoney: "",
          isErasable: true
        });
      } else if (val == 4) {
        this.curriculumInfo.chargeMode = 1;
        this.curriculumInfo.coursePackageEntityList = [];
        this.curriculumInfo.coursePackageEntityList.push({
          chargeMode: 1,
          courseTimeMode: 1,
          courseHour: 1,
          syllabusNum: "",
          cycle: 10,
          totalMoney: "",
          isErasable: true
        });
      } else {
        this.curriculumInfo.chargeMode = 1;
        this.curriculumInfo.coursePackageEntityList = [];
        this.curriculumInfo.coursePackageEntityList.push({
          chargeMode: 1,
          courseTimeMode: 1,
          courseHour: 1,
          syllabusNum: "",
          cycle: 10,
          totalMoney: "",
          isErasable: true
        });
      }
      vm.selectObject.courseType = val;
      vm.selectObject.subjectId = "";
      vm.selectObject.subSubjectId = "";
      vm.selectSubjectId = [];
      vm.curriculumInfo.courseName = "";
      vm.curriculumInfo.minSuitableAge = "";
      vm.curriculumInfo.maxSuitableAge = "";
      vm.getCourseLibraryList();
    },
    //删除库课程
    deleteCourse(index) {
      vm.isOrg = true;
      if (this.curriculumInfo.libraryCourseList.length == 1) {
        this.curriculumInfo.libraryCourseList = [];
        this.curriculumInfo.libraryCourseList.push({
          libraryCourseId: "",
          libraryCourseName: "",
          oneSyllabusDuration: "",
          perSyllabusDuration:"",
          syllabusNum: "",
          attributionType: 1
        });
      } else {
        this.curriculumInfo.libraryCourseList.splice(index, 1);
      }
    },
    //添加库课程
    addCourse() {
      this.curriculumInfo.libraryCourseList.push({
        libraryCourseId: "",
        libraryCourseName: "",
        oneSyllabusDuration: "",
        perSyllabusDuration:"",
        syllabusNum: "",
        attributionType: 1
      });
    },
    handleChange(item) {
      item.courseHour = item.syllabusNum;
    },
    valueChange(val) {
      // vm.curriculumInfo.coursePackageEntityList[0].courseHour =
      //   val * vm.curriculumInfo.coursePackageEntityList[0].oneSyllabusHour;
    },
    /**
     * 选择指定校区
     */
    schoolChange(val) {
      console.log(val);
      this.curriculumInfo.schoolText = [];
      this.curriculumInfo.courseCampusEntityList = [];
      for (let i = 0; i < val.length; i++) {
        this.curriculumInfo.courseCampusEntityList.push({
          campusId: val[i]
        });
        for (let y = 0; y < this.curriculumInfo.classList.length; y++) {
          if (val[i] == this.curriculumInfo.classList[y].campusId) {
            this.curriculumInfo.schoolText.push(
              this.curriculumInfo.classList[y].name
            );
          }
        }
      }
    },
    /**
     * 收费模式change
     */
    handleradioChange(val) {
      console.log(val);
      this.curriculumInfo.coursePackageEntityList = [];
      this.curriculumInfo.coursePackageEntityList.push({
        chargeMode: val,
        courseTimeMode: 1,
        courseHour: vm.maxCourseSum,
        syllabusNum: vm.maxCourseSum,
        cycle: 10,
        totalMoney: "",
        isErasable: true
      });
    },
    /**
     * 选择所有校区/指定校区
     */
    handleCheckedCitiesChange(value) {
      console.log(value);
      if (value == 1) {
        this.curriculumInfo.courseCampusEntityList = this.curriculumInfo.classList;
        this.curriculumInfo.isShowSchoolView = false;
        this.curriculumInfo.schoolText = ["所有校区"];
        this.curriculumInfo.selectSchoolList = [];
      } else {
        this.curriculumInfo.courseCampusEntityList = [];
        this.curriculumInfo.isShowSchoolView = true;
      }
    },
    /**
     * 删除收费标准
     */
    deletePrice(item) {
      console.log(item.chargeMode);
      var index = vm.curriculumInfo.coursePackageEntityList.indexOf(item);
      if (index !== -1) {
        vm.curriculumInfo.coursePackageEntityList.splice(index, 1);
      }
      console.log(vm.curriculumInfo.coursePackageEntityList.length);
      if (vm.curriculumInfo.coursePackageEntityList.length === 0) {
        vm.curriculumInfo.coursePackageEntityList.push({
          chargeMode: item.chargeMode,
          courseTimeMode: 1,
          courseHour: 1,
          syllabusNum: "",
          cycle: 10,
          totalMoney: "",
          isErasable: true
        });
      }
    },
    /**
     * 添加收费标准
     */
    addPrice(val) {
      this.curriculumInfo.coursePackageEntityList.push({
        chargeMode: val,
        courseTimeMode: 1,
        courseHour: 1,
        syllabusNum: "",
        cycle: 10,
        totalMoney: "",
        isErasable: true
      });
    },
    /**
     * 提交数据
     */
    submitForm() {
      if (this.curriculumInfo.courseType == "") {
        this.$message.warning("课程类别不能为空");
        return;
      }
      // if (
      //   "" + this.curriculumInfo.minSuitableAge == "" ||
      //   "" + this.curriculumInfo.maxSuitableAge == ""
      // ) {
      //   this.$message.warning("适用年龄不能为空");
      //   return;
      // }
      // if (this.curriculumInfo.subjectId == "") {
      //   this.$message.warning("科目不能为空");
      //   return;
      // }
      if (
        this.curriculumInfo.libraryCourseList.length == 1 &&
        this.curriculumInfo.libraryCourseList[0].libraryCourseName == ""
      ) {
        this.$message.warning("库课程不能为空");
        return;
      }
      if (this.curriculumInfo.coursePackageEntityList.length == 0) {
        this.$message.warning("收费标准不能为空");
        return;
      }
      for (
        let i = 0;
        i < this.curriculumInfo.coursePackageEntityList.length;
        i++
      ) {
        if (
          this.curriculumInfo.coursePackageEntityList[i].courseHour == "" ||
          this.curriculumInfo.coursePackageEntityList[i].courseHour == 0
        ) {
          this.$message.warning("收费标准中课时不能为零或空");
          return;
        }
        if (
          this.curriculumInfo.coursePackageEntityList[i].totalMoney == "" ||
          this.curriculumInfo.coursePackageEntityList[i].totalMoney == 0
        ) {
          this.$message.warning("收费标准中金额不能为零或空");
          return;
        }
      }
      if (this.curriculumInfo.courseCampusEntityList.length == 0) {
        this.$message.warning("开课校区不能为空");
        return;
      }
      for (let i = 0; i < this.curriculumInfo.libraryCourseList.length; i++) {
        if (
          !tool.judge(
            "courseName",
            this.curriculumInfo.libraryCourseList[i].libraryCourseName
          )
        ) {
          this.$message.warning("请输入有效的库课程名称");
          return;
        }
        if (
          this.curriculumInfo.libraryCourseList[i].perSyllabusDuration == ""
        ) {
          this.$message.warning("请输入课程时长");
          return;
        }
        if (
          this.curriculumInfo.libraryCourseList[i].perSyllabusDuration > 480
        ) {
          this.$message.warning("输入的课程时长不能大于480，请重新输入");
          this.curriculumInfo.libraryCourseList[i].perSyllabusDuration = "";
          return;
        }
        console.log(this.curriculumInfo.libraryCourseList[i].syllabusNum);
        if (this.curriculumInfo.libraryCourseList[i].syllabusNum == "") {
          this.$message.warning("请输入课程课节");
          return;
        }
        if(this.curriculumInfo.libraryCourseList[i].libraryCourseId == null){
          this.curriculumInfo.libraryCourseList[i].oneSyllabusDuration = this.curriculumInfo.libraryCourseList[i].perSyllabusDuration;
        }
      }
      if (
        this.curriculumInfo.courseType != 2 &&
        this.curriculumInfo.libraryCourseList[0].libraryCourseId == null
      ) {
        this.curriculumInfo.courseName = this.curriculumInfo.libraryCourseList[0].libraryCourseName;
      }
      if (this.curriculumInfo.courseName == "") {
        this.$message.warning("课程名称不能为空");
        return;
      }
      if (!tool.judge("courseName", this.curriculumInfo.courseName)) {
        this.$message.warning("请输入有效的课程名称");
        return;
      }
      let paras = {
        courseName: this.curriculumInfo.courseName,
        courseType: this.curriculumInfo.courseType,
        subjectId: "",
        courseMode: this.curriculumInfo.courseMode,
        chargeMode: this.curriculumInfo.chargeMode,
        offsetDay: this.curriculumInfo.offsetDay,
        createOrgId: this.curriculumInfo.createOrgId,
        remark: this.curriculumInfo.remark,
        minSuitableAge: this.curriculumInfo.minSuitableAge,
        maxSuitableAge: this.curriculumInfo.maxSuitableAge,
        coursePackageEntityList: this.curriculumInfo.coursePackageEntityList,
        courseCampusEntityList: [],
        libraryCourseList: this.curriculumInfo.libraryCourseList,
        consumeMode: this.curriculumInfo.consumeMode
      };
      if (vm.selectSubjectId.length == 1) {
        paras.subjectId = vm.selectSubjectId[0];
      } else if (vm.selectSubjectId.length == 2) {
        paras.subjectId = vm.selectSubjectId[0];
        paras.subSubjectId = vm.selectSubjectId[1];
      }
      this.curriculumInfo.courseCampusEntityList.forEach(item => {
        paras.courseCampusEntityList.push({
          campusId: item.campusId
        });
      });
      paras._wait = "";
      //courseCampusEntityList: this.curriculumInfo.courseCampusEntityList
      if (this.isAdd) {
        api.courseAdd(paras).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "新建课程成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      } else {
        paras.courseId = vm.curriculumInfo.courseId + "";
        api.courseUpdate(paras).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "编辑成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      }
    },
    cancel: function() {
      //隐藏弹窗
      vm.open = false;
      setTimeout(function() {
        vm.show = false;
        vm.visible = false;
        vm.selectObject = {
          campusId: "",
          subjectId: "",
          courseType: "",
          subSubjectId: ""
        };
        vm.courseLibraryList = [];
        vm.curriculumInfo = {};
      }, 500);
    }
  },
  computed: {
    activeChargeModeList: function() {
      return this.chargeModeList.filter(function(item) {
        return item.value != 1 && item.value != 2;
      });
    },
    activeModeList: function() {
      return this.chargeModeList.filter(function(item) {
        return item.value == 1;
      });
    }
  },
  created: function() {
    vm = this;
    for (let i = 0; i < 100; i++) {
      vm.ageStartList.push({ name: "" + i, value: "" + i, show: true });
      vm.ageEndList.push({ name: "" + i, value: "" + i, show: true });
    }
  }
};
</script>
<style lang="scss">
.newCourse {
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .el-dialog-bg {
    background: #ffffff;
    width: 9.96rem;
    border-radius: 0.1rem;
  }
  .panel_title {
    font-size: 0.22rem;
    color: #333333;
    padding: 0.03rem 0.07rem 0rem 0.1rem;
  }
  .ruleForm {
    font-size: 16px;
    color: #666666;
  }
  .el-form-item__label {
    font-size: 16px;
    color: #666666;
  }

  /*底部按钮*/
  .btn_div {
    width: 3.5rem;
    margin: 0 auto;
    padding: 0.3rem 0 0 0;
    overflow: hidden;
  }
  .btn_div > div {
    width: 1.6rem;
    height: 0.4rem;
    line-height: 0.4rem;
    float: left;
    font-size: 0.16rem;
    text-align: center;
    border-radius: 0.05rem;
    cursor: pointer;
    user-select: none;
  }

  .btn_disable {
    color: #999999;
    background-color: #dedede;
    margin-right: 0.3rem;
    cursor: not-allowed !important;
  }
  .btn_sure {
    color: white;
    background-color: #3885f8;
    margin-right: 0.3rem;
  }
  .btn_sure:hover {
    background-color: rgba(56, 133, 248, 0.8);
  }
  .btn_cancel {
    color: #666666;
    border: 0.01rem solid #c0c4cc;
  }
  .btn_cancel:hover {
    background-color: rgba(56, 133, 248, 0.1);
  }
  .el-form-item {
    margin-bottom: 0.1rem;
  }
  .line_from {
    height: 1px;
    margin: 0.24rem 0.18rem 0.18rem 0.18rem;
    background-color: #f3f6fc;
  }
  .charge_item {
    font-size: 16px;
    color: #333333;
  }
  .school_item {
    font-size: 14px;
    border-radius: 0.05rem;
    background-color: #ebf5ff;
    color: #409eff;
    border: 0.01rem solid #cde6ff;
    padding: 0.05rem 0.1rem;
    margin-right: 0.1rem;
  }
  .newclassroomNumber {
    .el-input__inner {
      border-radius: 0.04rem !important;
    }
  }
}
</style>
<style scoped src="@/style/clue_manage/clue_add.css"></style>
<style scoped src="@/style/module/dialog.css"></style>
<style scoped>
.panel {
  width: 10rem;
  padding-bottom: 0.2rem;
}
</style>

